package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;

/**
 * 折叠菜单
 * @author rebin
 *
 */
public class Accordion extends NokaTag{
	private static final long serialVersionUID = 9116999168131312272L;
	
	private String id=null;
	private Integer width=150;
	private Integer hwidth=width;
	private Integer contentwidth=hwidth;
	private Integer height=200;
	private String open="0";//-1 表示展开所有,多个用逗号分开，如0,1,2
	private Integer type=0;
	private String align="center";
	private Integer styleid=1;
	
	public int doStartTag() throws JspException {
		JspWriter jspOut = pageContext.getOut();
		StringBuilder bodystr = new StringBuilder();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		bodystr.append(potjs(request));//js框架
		bodystr.append(treejs(request));//tree js
		bodystr.append(AccordionJS(request));
		Concest.PAGETEMP.put("accordion.style.css_id",id+"  ");//注册
		Concest.PAGETEMP.put("accordion.style.css_width",width.toString());//注册
		if(IsIE(request)){
			Concest.PAGETEMP.put("accordion.style.css_hwidth",hwidth.toString());//注册
			Concest.PAGETEMP.put("accordion.style.css_contentwidth",contentwidth.toString());//注册
		}else{
			Concest.PAGETEMP.put("accordion.style.css_hwidth",String.valueOf(hwidth-14));//注册
			Concest.PAGETEMP.put("accordion.style.css_contentwidth",String.valueOf(contentwidth-(18+14)));//注册
		}
		Concest.PAGETEMP.put("accordion.style.css_align",align);//注册
		Concest.PAGETEMP.put("accordion.style.css_height",height.toString());//注册
		bodystr.append(accordionCss(styleid,request));
		bodystr.append("<ul class=\""+id+"\" id=\""+id+"\">");
		try {
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		try {
				StringBuilder bodystr = new StringBuilder();
				bodystr.append(" </ul>");
				bodystr.append(" <script type=\"text/javascript\">");
				bodystr.append(" var nestedAccordion=new TINY.accordion.slider(\"nestedAccordion\");");
				bodystr.append(" nestedAccordion.init(\""+id+"\",\"h3\","+type+",["+open+"],\"acc-selected\");");
				bodystr.append(" </script>");
				pageContext.getOut().write(bodystr.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return EVAL_PAGE;
	}
	public boolean IsIE(HttpServletRequest request){
		return ( (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie")!=-1 || request.getHeader("user-agent").toLowerCase().indexOf("msie")!=-1) );
		
	}
	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.hwidth=width;
		this.contentwidth=hwidth;
		this.width = width;
	}

	public String getOpen() {
		return open;
	}

	public void setOpen(String open) {
		this.open = open;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public Integer getStyleid() {
		return styleid;
	}

	public void setStyleid(Integer styleid) {
		this.styleid = styleid;
	}
}
